﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JMT.Controls.Relations.LinkNodes
{
    /// <summary>
    /// JAggregationNode.xaml 的交互逻辑
    /// </summary>
    public partial class JAggregationNode : LinkNode
    {
        public JAggregationNode()
        {
            InitializeComponent();
        }

        public Point GetVertex()
        {
            return new Point(0, 10);
        }

        public void SetVertex(Point pos)
        {
            Canvas.SetTop(this, pos.Y);
            Canvas.SetLeft(this, pos.X - this.Width / 2);
        }

        public void SetDirection(Point fromPos, Point toPos)
        {
            double deltaY = fromPos.Y - toPos.Y;
            double deltaX = fromPos.X - toPos.X;

            if (deltaY == 0)
            {
                if (deltaX > 0)
                {
                    (this.RenderTransform as RotateTransform).Angle = 270;
                }
                else if (deltaX < 0)
                {
                    (this.RenderTransform as RotateTransform).Angle = 90;
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            else if (deltaY > 0)
            {
                if (deltaX > 0)
                {
                    double angle = -180 * (Math.Atan(Math.Abs(deltaX / deltaY)) / Math.PI);
                    (this.RenderTransform as RotateTransform).Angle = angle;
                }
                else if (deltaX < 0)
                {
                    double angle = 180 * (Math.Atan(Math.Abs(deltaX / deltaY)) / Math.PI);
                    (this.RenderTransform as RotateTransform).Angle = angle;
                }
                else
                {
                    (this.RenderTransform as RotateTransform).Angle = 0;
                }
            }
            else if (deltaY < 0)
            {
                if (deltaX > 0)
                {
                    double angle = 270 - 180 * (Math.Atan(Math.Abs(deltaY / deltaX)) / Math.PI);
                    (this.RenderTransform as RotateTransform).Angle = angle;
                }
                else if (deltaX < 0)
                {
                    double angle = 90 + 180 * (Math.Atan(Math.Abs(deltaY / deltaX)) / Math.PI);
                    (this.RenderTransform as RotateTransform).Angle = angle;
                }
                else
                {
                    (this.RenderTransform as RotateTransform).Angle = 180;
                }
            }
        }
    }
}
